package thread;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;


public class Test02 {


    public static void main(String[] args) {
        //1.把线程不安全的顺序表变成线程安全的
        ArrayList<Integer> arrayList = new ArrayList<>();
        Collections.synchronizedList(arrayList);
        //2.
        

    }

    static int count=0;
    public static void main5(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(1);
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                try {
                    semaphore.acquire();
                    count++;
                    semaphore.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                try {
                    semaphore.acquire();
                    count++;
                    semaphore.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count="+count);
    }



    public static void main4(String[] args) throws InterruptedException {

        ReentrantLock locker = new ReentrantLock(); //创建锁对象
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                try {
                    //1.加锁
                    locker.lock();
                    count++;
                }finally {
                    locker.unlock();
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                try {
                    //1.加锁
                    locker.lock();
                    count++;
                }finally {
                    locker.unlock();
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count="+count);

    }

    public static void main3(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("我是定时器，5S后执行");
            }
        },5000);

    }



    public static void main2(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int count = i;
            executorService.submit(()->{
                System.out.println("第"+count+"个任务");
            });
        }
    }
    public static void main1(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
        queue.put(10);
        System.out.println(queue.take());

    }

}
